<?php

namespace app\modules\admin\controllers;
use app\models\Card;
use app\models\Agent;
use app\models\UserCardRecharge;
use Yii;
use yii\web\Response;
use yii\web\ServerErrorHttpException;

/**
 * 管理后台默认控制器
 * Class DefaultController
 * @package app\modules\admin\controllers
 */
class DefaultController extends BaseController
{
    /**
     * 管理后台主页
     * @return string
     */
    public function actionIndex()
    {   
        //今天时间戳
        $start_time = strtotime(date('Y-m-d'));
        $end_time = $start_time + 86399;
        //昨天时间戳
        $yesterday_start_time = strtotime(date('Y-m-d')) - 86400;
        $yesterday_end_time = $yesterday_start_time + 86399;
        //本月时间戳
        $month = date('Y-m-01');
        $month_start = strtotime($month);
        $month_end = strtotime(date('Y-m-d',strtotime("$month +1 month -1 day"))) + 86399; 

        //上月时间戳
        $last_month_start = strtotime(date('Y-m-d',strtotime("$month -1 month")));
        $date_start = date('Y-m-d',$last_month_start);
        $last_month_end = strtotime(date('Y-m-d',strtotime("$date_start +1 month -1 day"))) + 86399; 
        
        $total_card_count = Card::find()->count();
        $agent_count = Agent::find()->count();
        $recharge_count = UserCardRecharge::find()->where(['is_show'=>1])->count();
        $active_card_count = Card::find()->andFilterWhere(['<>','term_end_time',0])->count();
        $today_recharge_count = UserCardRecharge::find()->where(['between','create_time',$start_time,$end_time])->andFilterWhere(['<','recharge_type',4])->count();
        $yesterday_recharge_count = UserCardRecharge::find()->where(['between','create_time',$yesterday_start_time,$yesterday_end_time])->andFilterWhere(['<','recharge_type',4])->count();
        $month_recharge_count = UserCardRecharge::find()->where(['between','create_time',$month_start,$month_end])->andFilterWhere(['<','recharge_type',4])->count();
        $last_month_recharge_count = UserCardRecharge::find()->where(['between','create_time',$last_month_start,$last_month_end])->andFilterWhere(['<','recharge_type',4])->count();
        $today_active_count = Card::find()->where(['status'=>4])->where(['between','active_time',$start_time,$end_time])->count();
        $yesterday_active_count = Card::find()->where(['status'=>4])->where(['between','active_time',$yesterday_start_time,$yesterday_end_time])->count();
        $month_active_count = Card::find()->where(['status'=>4])->where(['between','active_time',$month_start,$month_end])->count();
        $last_month_active_count = Card::find()->where(['status'=>4])->where(['between','active_time',$last_month_start,$last_month_end])->count();
        return $this->render('index',[
            'total_card_count'              => $total_card_count,
            'agent_count'                   => $agent_count,
            'recharge_count'                => $recharge_count,
            'active_card_count'             => $active_card_count,
            'today_recharge_count'          => $today_recharge_count,
            'yesterday_recharge_count'      => $yesterday_recharge_count, 
            'month_recharge_count'          => $month_recharge_count,
            'last_month_recharge_count'     => $last_month_recharge_count,
            'today_active_count'            => $today_active_count,
            'yesterday_active_count'        => $yesterday_active_count,
            'month_active_count'            => $month_active_count,
            'last_month_active_count'       => $last_month_active_count,
        ]);
    }

    /**
     * 错误页
     * 自动读取错误内容，页面访问的错误直接显示错误页面，AJAX请求则返回格式统一的JSON
     * {
     *     "result":"failure",
     *     "message":"错误提示。",
     *     "errors":...详细错误信息，一般只用来跟踪处理，不作为对用户展示...
     * }
     * @throws ServerErrorHttpException
     * @return array|string
     */
    public function actionError()
    {
        $exception = Yii::$app->errorHandler->exception;
        if ($exception !== null) {
            if ($this->isAjax()) {
                Yii::$app->response->format = Response::FORMAT_JSON;
                return [
                    'message'=>$exception->getMessage(),
                    'errors'=>YII_ENV_DEV ? $exception->getTraceAsString() : ''
                ];
            } else {
                return $this->render('error', [
                    'exception'=>$exception
                ]);
            }
        }
        throw new ServerErrorHttpException('没有找到任何错误信息。');
    }
}
